<template>
	<view class="container">
		<view class="header">
			<view class="title-section">
				<text class="title">我的成就</text>
				<text class="total-count">{{ totalAchievements }}个成就</text>
			</view>
			<view class="progress-section">
				<view class="progress-bar">
					<view class="progress-fill" :style="{ width: achievementRate + '%' }"></view>
				</view>
				<text class="progress-text">完成率 {{ achievementRate }}%</text>
			</view>
		</view>
		
		<view class="achievement-list">
			<view v-for="(group, game) in groupedAchievements" :key="game" class="game-group">
				<view class="game-header">
					<image :src="group.logo" class="game-logo" mode="aspectFill" />
					<view class="game-info">
						<text class="game-name">{{ group.name }}</text>
						<text class="game-progress">{{ group.completed }}/{{ group.total }}个成就</text>
					</view>
				</view>
				
				<view class="achievements">
					<view v-for="achievement in group.items" :key="achievement.id" 
						class="achievement-item" :class="{ 'locked': !achievement.unlocked }">
						<image :src="achievement.icon" class="achievement-icon" mode="aspectFill" />
						<view class="achievement-info">
							<text class="achievement-name">{{ achievement.name }}</text>
							<text class="achievement-desc">{{ achievement.description }}</text>
							<view class="unlock-info" v-if="achievement.unlocked">
								<text class="unlock-date">{{ achievement.unlockDate }}</text>
								<text class="unlock-rate">{{ achievement.unlockRate }}%的玩家已解锁</text>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		
		<view class="empty-state" v-if="Object.keys(groupedAchievements).length === 0">
			<image src="/static/empty-achievement.png" mode="aspectFit" class="empty-image" />
			<text class="empty-text">还没有获得任何成就哦</text>
		</view>
	</view>
</template>

<script>
import { games } from '../../../utils/gameData.js';

export default {
	data() {
		return {
			groupedAchievements: {},
			totalAchievements: 0,
			achievementRate: 0
		}
	},
	created() {
		this.generateMockData();
	},
	methods: {
		generateMockData() {
			let totalUnlocked = 0;
			let totalCount = 0;
			
			// 为前3个游戏生成成就数据
			const selectedGames = games.slice(0, 3);
			this.groupedAchievements = selectedGames.reduce((groups, game) => {
				const achievementCount = Math.floor(Math.random() * 5) + 5; // 5-10个成就
				const unlockedCount = Math.floor(Math.random() * achievementCount); // 随机解锁数量
				
				totalCount += achievementCount;
				totalUnlocked += unlockedCount;
				
				const achievements = Array(achievementCount).fill().map((_, index) => {
					const isUnlocked = index < unlockedCount;
					return {
						id: `${game.game_id}_${index}`,
						name: this.getRandomAchievementName(),
						description: this.getRandomDescription(),
						icon: game.game_logo, // 使用游戏logo作为成就图标
						unlocked: isUnlocked,
						unlockDate: isUnlocked ? this.getRandomDate() : null,
						unlockRate: Math.floor(Math.random() * 100)
					};
				});
				
				groups[game.game_id] = {
					name: game.game_name,
					logo: game.game_logo,
					completed: unlockedCount,
					total: achievementCount,
					items: achievements
				};
				
				return groups;
			}, {});
			
			this.totalAchievements = totalUnlocked;
			this.achievementRate = Math.floor((totalUnlocked / totalCount) * 100);
		},
		getRandomAchievementName() {
			const names = ['初次尝试', '熟能生巧', '登峰造极', '完美通关', '速通达人', 
				'收集狂魔', '隐藏技巧', '连续获胜', '稀有发现', '终极挑战'];
			return names[Math.floor(Math.random() * names.length)];
		},
		getRandomDescription() {
			const descriptions = [
				'首次完成游戏',
				'累计游戏时间达到10小时',
				'获得100次胜利',
				'收集所有道具',
				'不死亡通关',
				'发现隐藏关卡',
				'连续获胜10次',
				'达到最高等级',
				'解锁所有角色',
				'完成所有支线任务'
			];
			return descriptions[Math.floor(Math.random() * descriptions.length)];
		},
		getRandomDate() {
			const start = new Date(2023, 0, 1);
			const end = new Date();
			const date = new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
			return `${date.getMonth() + 1}月${date.getDate()}日`;
		}
	}
}
</script>

<style scoped>
.container {
	min-height: 100vh;
	background: #1E1E2E;
	padding: 12px;
}

.header {
	background: #2D2D44;
	border-radius: 6px;
	padding: 15px;
	margin-bottom: 15px;
}

.title-section {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 12px;
}

.title {
	font-size: 18px;
	color: #9796B8;
	font-weight: bold;
}

.total-count {
	font-size: 14px;
	color: #6E6C89;
}

.progress-section {
	display: flex;
	flex-direction: column;
	gap: 6px;
}

.progress-bar {
	height: 6px;
	background: #1E1E2E;
	border-radius: 3px;
	overflow: hidden;
}

.progress-fill {
	height: 100%;
	background: #9796B8;
	border-radius: 3px;
	transition: width 0.3s ease;
}

.progress-text {
	font-size: 12px;
	color: #6E6C89;
	text-align: right;
}

.game-group {
	background: #2D2D44;
	border-radius: 6px;
	margin-bottom: 15px;
	overflow: hidden;
}

.game-header {
	display: flex;
	align-items: center;
	padding: 15px;
	border-bottom: 1px solid rgba(110, 108, 137, 0.2);
}

.game-logo {
	width: 40px;
	height: 40px;
	border-radius: 4px;
	margin-right: 12px;
}

.game-info {
	flex: 1;
}

.game-name {
	font-size: 16px;
	color: #9796B8;
	margin-bottom: 4px;
}

.game-progress {
	font-size: 12px;
	color: #6E6C89;
}

.achievements {
	padding: 0 15px;
}

.achievement-item {
	display: flex;
	padding: 15px 0;
	border-bottom: 1px solid rgba(110, 108, 137, 0.1);
}

.achievement-item:last-child {
	border-bottom: none;
}

.achievement-item.locked {
	opacity: 0.5;
}

.achievement-icon {
	width: 36px;
	height: 36px;
	border-radius: 3px;
	margin-right: 12px;
}

.achievement-info {
	flex: 1;
}

.achievement-name {
	font-size: 14px;
	color: #9796B8;
	margin-bottom: 4px;
}

.achievement-desc {
	font-size: 12px;
	color: #6E6C89;
	margin-bottom: 6px;
}

.unlock-info {
	display: flex;
	justify-content: space-between;
	font-size: 11px;
	color: #6E6C89;
}

.empty-state {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	padding: 50px 0;
}

.empty-image {
	width: 150px;
	height: 150px;
	margin-bottom: 15px;
}

.empty-text {
	color: #6E6C89;
	font-size: 14px;
}
</style> 